import 'package:flutter/material.dart';
import 'package:provide/provide.dart';
import '../provide/cart.dart';
import 'cart_page/cart_item.dart';
import './cart_page/cart_bottom.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class CartPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('购物车'),
      ),
      body: FutureBuilder(
      future:_getCartInfo(context),
      builder: (context,snapshot){
        if(snapshot.hasData){
          print(snapshot.data);
          return Provide<CartProvide>(
            builder: (context,child,val){
              if(val.allGoodsCount != 0){
                return Stack(
                  children: <Widget>[
                    Provide<CartProvide>(
                        builder: (context,child,cartprovide){
                          List cartList= Provide.value<CartProvide>(context).cartList;
                          return 
                            ListView(
                              children: <Widget>[
                                SizedBox(
                                  height: 50,
                                ),
                                Column(
                                  children: cartList.map((i){
                                    return CartItem(i);
                                  }).toList(),
                                ),
                                SizedBox(
                                  height: ScreenUtil().setHeight(140),
                                )
                              ],
                            );
                        }
                      ),
                    Positioned(
                      bottom:0,
                      left:0,
                      child: Provide<CartProvide>(
                        builder: (context,child,cartprovide){
                          return CartBottom();
                        },
                      ),
                    ),
                    Positioned(
                      top: 10,
                      right: 10,
                      child: InkWell(
                        onTap: (){
                          Provide.value<CartProvide>(context).remove();
                        },
                        child: Container(
                          padding: EdgeInsets.all(10.0),
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                            color: Colors.red,
                            borderRadius: BorderRadius.circular(3.0)
                          ),
                          child: Text(
                            '清空购物车',
                            style: TextStyle(
                              color: Colors.white
                            ),
                          ),
                        ),
                      )
                    )
                  ],
                );
              }else {
                return Center(
                  child: Text('购物车没东西呦'),
                );
              }
            },
          );
        }
        else{
          return Center(
            child: Text('购物车没东西呦'),
          );
        }
      },
    ),
    );
  }

  // 取出本地持久化数据
  Future _getCartInfo(BuildContext context) async{
     await Provide.value<CartProvide>(context).getCartInfo();
     return Provide.value<CartProvide>(context).cartList.isNotEmpty?
      Provide.value<CartProvide>(context).cartList
      :null;
  }
}